package com.hermit.qmst.UI.activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.hermit.qmst.R;
import com.hermit.qmst.tools.Common;
import com.hermit.qmst.tools.Log;
import com.hermit.qmst.tools.ParseXML;
import com.hermit.qmst.tools.Preferences;

/**
 * Created by hermithermit on 15/2/17.
 */
public class MyAccountActivity extends Activity implements View.OnClickListener{

    private TextView mtvPhone;
    private TextView mtvBalance;
    private TextView mtvValidday;
    private ProgressDialog mProgressDialog;
    private Preferences preferences;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_account);
        preferences=new Preferences(MyAccountActivity.this);
        initView();
        request_balance();
    }



    private void initView(){

       ((TextView)findViewById(R.id.tv_title)).setText("我的余额");
       ((ImageView)findViewById(R.id.iv_title_left)).setImageResource(R.drawable.ic_back);
       findViewById(R.id.iv_title_left).setOnClickListener(this);

        mtvPhone = (TextView)findViewById(R.id.tv_phone);
        mtvPhone.setText(Common.myPhone);
        mtvBalance = (TextView)findViewById(R.id.tv_blance);
        mtvValidday = (TextView)findViewById(R.id.tv_validay);
    }

    private void request_balance(){
        mProgressDialog = ProgressDialog.show(this,"提示","系统正在处理，请稍后…",false,false);
        RequestQueue requestQueue = Volley.newRequestQueue(this);



        String url = Common.submitQueryBalancePath() + "regnum=" + Common.myPhone+"&agent_id="+Common.mAgentId;

        StringRequest request = new StringRequest(Request.Method.POST,url,new Response.Listener<String>(){

            @Override
            public void onResponse(String s) {
                Log.i("MyAccountActivity",s,true);
                if (s != null && s.length() > 0) {

                    if (ParseXML.parseResponseXML(s,"Ret").equals("0")) {
                        try {
                            String balance = ParseXML.parseResponseXML(s,"Val");
                            String date = ParseXML.parseResponseXML(s,"Date");
                            mtvBalance.setText(balance);
                            mtvValidday.setText(date);

                        } catch (Exception e) {
                            // TODO: handle exception
                        }
                    } else {
                        Toast.makeText(MyAccountActivity.this, "查询失败", Toast.LENGTH_LONG).show();
                    }
                    mProgressDialog.dismiss();
                }

            }
        },new Response.ErrorListener(){
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(MyAccountActivity.this,"请求失败",Toast.LENGTH_SHORT).show();
                mProgressDialog.dismiss();
            }
        });


        requestQueue.add(request);


    }


    @Override
    public void onClick(View v) {

        switch (v.getId()){
            case R.id.iv_title_left:

                MyAccountActivity.this.finish();
                break;
            default:
                break;
        }

    }
}
